﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Incendios0_1.Camera.wpfdatamanager;

namespace Incendios0_1.Camera.camera_management
{
    /// <summary>
    /// Interaction logic for Add_camera.xaml
    /// </summary>
    public partial class Add_camera : Window
    {
        public CameraData camera { get; set; }
        Boolean newUser;
        public Add_camera()
        {
            InitializeComponent();
            camera = new CameraData();
            camera.Name = Guid.NewGuid().ToString();
            camera.Type = 0;
            newUser = true;
            main.DataContext = camera;
        }

        public Add_camera(CameraData cam)
        {
            InitializeComponent();
            this.camera = cam;
            newUser = false;
            main.DataContext = camera;
        }

        private void add_camera_Click(object sender, RoutedEventArgs e)
        {
            if (newUser)
            {
                CameraManager.AddCamera(camera);
                newUser = false;
            }
            if (CameraManager.SaveChanges()==null)
                Microsoft.Windows.Controls.MessageBox.Show("La camara ya existe", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            else
                Microsoft.Windows.Controls.MessageBox.Show("La cámara ha sido guardada", "Guardar", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void cancel_add_camera_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Cancel()
        {
            if (!newUser)
            {
                CameraManager.Cancel(camera);
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Cancel();
        }

        private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!AreAllValidNumericChars(e.Text))
            {
                e.Handled = true;
            }
        }

        private void textBox2_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!AreAllValidNumericChars(e.Text))
            {
                e.Handled = true;
            }
        }

        private void textBox3_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!AreAllValidNumericChars(e.Text))
            {
                e.Handled = true;
            }
        }

        private bool AreAllValidNumericChars(string str)
        {
            bool ret = true;
            if (str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyGroupSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencySymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeSign |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeInfinitySymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberGroupSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentDecimalSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentGroupSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentSymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PerMilleSymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveInfinitySymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveSign)
                return ret;

            int l = str.Length;
            for (int i = 0; i < l; i++)
            {
                char ch = str[i];
                ret &= Char.IsDigit(ch);
            }

            return ret;
        }
    }
}
